package ro.toporjinschi.anytaxi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import ro.toporjinschi.androidorm.EntityManager;
import ro.toporjinschi.anytaxi.database.DatabaseAccess;
import ro.toporjinschi.anytaxi.database.beans.Place;

import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

public class Tabs extends FragmentActivity implements TabHost.OnTabChangeListener, MenuItem.OnMenuItemClickListener {
    private TabHost tabHost;
    private FragmentManager fm;
    private final static String SELECTED_TAG = "selected";
    private TabManager tabManager = new TabManager();
    private DatabaseAccess db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tabs);
        //        db = ((AnyTaxi) getApplication()).getDb();
        fm = getSupportFragmentManager();
        tabHost = (TabHost) findViewById(android.R.id.tabhost);
        tabHost.setup();
        setTabIndicator();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.tabs, menu);

        MenuItem dumpDb = menu.findItem(R.id.dumpDb);
        dumpDb.setOnMenuItemClickListener(this);

        MenuItem populatePlaces = menu.findItem(R.id.populatePlaces);
        populatePlaces.setOnMenuItemClickListener(this);
        return true;
    }

    private void setTabIndicator() {
        // adding tabs indicator
        List<TabDetails> tabs = tabManager.getTabs();
        for (int i = 0; i < tabs.size(); i++) {
            addTab(tabs.get(i));
        }

        // adding content tab
        FragmentTransaction ft = fm.beginTransaction();
        ft.add(android.R.id.tabcontent, tabManager.getFragmentOfFirstTab(), SELECTED_TAG);
        ft.commit();
        tabHost.setOnTabChangedListener(this);
    }

    private void addTab(TabDetails tab) {
        TabHost.TabSpec spec = tabHost.newTabSpec(tab.getTag());
        TabWidget tw = (TabWidget) findViewById(android.R.id.tabs);
        View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, tw, false);
        TextView title = (TextView) tabIndicator.findViewById(R.id.title);
        title.setText(tab.getTextId());
        ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
        icon.setImageResource(tab.getIconId());
        spec.setIndicator(tabIndicator);
        spec.setContent(new DummyTabContent(getBaseContext()));
        tabHost.addTab(spec);
    }

    @Override
    public void onTabChanged(String tabId) {
        FragmentTransaction ft = fm.beginTransaction();
        Fragment fr = fm.findFragmentByTag(SELECTED_TAG);
        ft.remove(fr);
        ft.replace(android.R.id.tabcontent, tabManager.getFragmentForTag(tabId), SELECTED_TAG);
        ft.addToBackStack(null);
        ft.commit();
    }

    @Override
    public boolean onMenuItemClick(MenuItem mi) {
        if (mi.getItemId() == R.id.dumpDb) {
            copyDataBase();
        } else if (mi.getItemId() == R.id.populatePlaces) {
            addPlacesTestData();
        }
        return true;
    }

    private void copyDataBase() {
        File f = new File("/data/data/ro.toporjinschi.anytaxi/databases/anyTaxi.db3");
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream(f);
            fos = new FileOutputStream("/storage/sdcard0/db/anytaxi.db3");
            while (true) {
                int i = fis.read();
                if (i != -1) {
                    fos.write(i);
                } else {
                    break;
                }
            }
            fos.flush();
            Toast.makeText(this, "DB dump OK", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(this, "DB dump ERROR", Toast.LENGTH_LONG).show();
        } finally {
            try {
                fos.close();
                fis.close();
            } catch (IOException ioe) {
            }
        }

    }

    private void addPlacesTestData() {

        EntityManager em = EntityManager.getInstance();
        em.beginTransaction();
        for (int i = 0; i < 100; i++) {

            Place p = new Place();
            p.setTitle("Titlu" + i);
            p.setBuilding("building" + i);
            p.setDetails("Details" + i);
            p.setNo(i);
            p.setStairs("Stairs" + i);
            p.setStreat("Streat" + i);
            p.setTown("Town" + i);
            p.setZipCode(i * 100);
            em.insert(p);
        }
        em.endTransaction();
    }
}
